#include "myfastdo/myfastdo.hpp"

USING_ALL;
BEGIN_WEB_MAIN
// RSP.header["Access-Control-Allow-Headers"] = "*";
// RSP.header["Access-Control-Allow-Origin"] = "*";
// RSP.header["Access-Control-Allow-Methods"] = "POST, GET, PUT, OPTIONS, DELETE";
RSP.setCharset("utf-8");
String action = "";
String path = "";
winux::String head = "";
String method = REQ.header.getMethod();
SERV.log("method: " + method);
if (method == "OPTIONS")
{
    return 0;
}

SERV.log("get: " + REQ.get.getVars().json());
SERV.log("post: " + REQ.post.getVars().json());

try
{
    switch (SERV.data.reqType())
    {
    case RequestType::Get:
    {
        action = SERV.data.get<String>("action", "");
        path = SERV.data.get<String>("path", "actions");
        head = SERV.actionHeadGet;
    }
    break;
    case RequestType::Post:
    {
        action = SERV.data.post<String>("action", "");
        path = SERV.data.post<String>("path", "actions");
        head = SERV.actionHeadPost;
    }
    break;
    case RequestType::Unknown:
    {
        throw RequestError("REQ_ILL", "请求既不是GET也不是POST");
    }
    break;
    }

    SERV.log("请求路径:" + path + "/" + head + action + ".mdo");
    if (!SERV.runAction(path, action))
    {
        throw RequestError("MOD_NOT_FOUND");
    }
}
catch (RequestError &e)
{
    SERV.data.error(e);
}
catch (Error &e)
{
    SERV.data.error("ERROR", e.what());
}
catch (...)
{
    SERV.data.error("UNKNOWN_ERROR", "");
}
SERV.data.response();
if (SERV.data.res.ret == 0)
{
    SERV.log("返回:" + SERV.data.res.json());
}
else
{
    SERV.error("请求失败:" + SERV.data.res.json());
}
END_WEB_MAIN